package com.ax.base.drugproducer.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import com.ax.base.common.utils.R;
import com.ax.base.drugproducer.domain.BaseDrugClass;
import com.ax.base.drugproducer.service.DrugClassService;
import com.ax.base.system.domain.MenuDO;

@Controller
@RequestMapping("/sys/drugClass")
public class DrugClassController {

	@Autowired
	DrugClassService drugClassService;
	
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping()
	String drugAdministration(Model model) {
		return "drugproducer/drugclass/drugclass";
	}
	
	/**
	 * 药品分类查询(模块:药品分类)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return List<BaseDrugClass>
	 */
	@RequestMapping("/queryDrugclass")
	@ResponseBody
	public List<BaseDrugClass> queryDrugclass(AjaxRes ajaxRes,@RequestParam Map<String,Object> params) {
			   List<BaseDrugClass> BaseDrugClass = this.drugClassService.queryDrugclassSer(params);
			   return BaseDrugClass;
	  }
	
	/**
	 * 药品分类增加(模块:药品分类)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseDrugClass baseDrugClass
	 */
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Long pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName",drugClassService.getBaseDrugClass(pId).getName());
		}
		return "drugproducer/drugclass/add";
	}
	
	/**
	 * 药品分类增加(模块:药品分类)
	 * 
	 * @param BaseDrugClass baseDrugClass
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertBaseDrugClass")
	@ResponseBody
	public AjaxRes insertDrugDep(AjaxRes ajaxRes,BaseDrugClass baseDrugClass){
		try {
			this.drugClassService.insertBaseDrugClass(baseDrugClass);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 药品分类(模块:药品分类)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return BaseDrugClass baseDrugClass
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("drugClass",drugClassService.getBaseDrugClass(id));
		return "drugproducer/drugclass/edit";
	}
	
	/**
	 * 药品分类修改(模块:药品分类)
	 * 
	 * @param BaseDrugClass baseDrugClass
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateBaseDrugClass")
	@ResponseBody
	public AjaxRes updateBaseDrugClass(AjaxRes ajaxRes,BaseDrugClass baseDrugClass){
		try {
			this.drugClassService.updateBaseDrugClassSer(baseDrugClass);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * Tree
	 * 
	 * @param Long parentClassId
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/tree/{parentClassId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("parentClassId") Long parentClassId) {
		Tree<MenuDO> tree = drugClassService.getTree(parentClassId);
		return tree;
	}
	
	/**
	 * 验证类型名称是否重复(新增)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@PostMapping("/validate")
	@ResponseBody
	public boolean validate(String parameter){
		return !drugClassService.validateSer(parameter);
	}
	
	/**
	 * 验证类型名称是否重复(修改)
	 * 
	 * @param String parametero
	 *         
	 * @return boolean
	 */
	@PostMapping("/updateValidate")
	@ResponseBody
	public boolean updateValidate(String parameter){
		return !drugClassService.updateValidateSer(parameter);
	}
}
